package leet.contest;

import common.TreeNode;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * @author fance
 * @date 2018/7/27 8:37
 */
public class Week94 {
    public boolean leafSimilar(TreeNode root1, TreeNode root2) {
        List<Integer> res1 = new ArrayList<>();
        List<Integer> res2 = new ArrayList<>();
        pre(root1,res1);
        pre(root2,res2);
        if (res1.size() != res2.size()) {
            return false;
        }
        for (int i = 0; i < res1.size(); i++) {
            if (!Objects.equals(res1.get(i), res2.get(i))) {
                return false;
            }
        }
        return true;
    }
    private void pre(TreeNode root, List<Integer> res) {
        if (root == null) {
            return;
        }
        if (root.left == null && root.right == null) {
            res.add(root.val);
        }
        if (root.left != null) {
            pre(root.left,res);
        }
        if (root.right != null) {
            pre(root.right,res);
        }
    }
}
